using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
namespace Gadgets
{
    public partial class ReportMenu : DockContent 
    {
        public ReportMenu()
        {
            InitializeComponent();
        }
        public void Restart()
        {            
            DataSet menus = ABSCommonLibrary.DoInquiry(
                 CommonLibrary.CommonLibrary.BOSC020001, new object[2] { DeclareLibrary.USER_NAME, "R" });
            treeView1.Nodes.Clear();
            if (menus != null)
            {
                foreach (DataRow row in menus.Tables[0].Rows)
                {
                    if (Convert.ToInt32(row["PARENT_ID"]) == 0)
                    {
                        TreeNode root = new TreeNode();
                        root.Text = row["MENU_TEXT"].ToString();
                        root.Name = row["MENU_ID"].ToString();
                        treeView1.Nodes.Add(root);
                        AddTreeNode(ref root, menus);
                    }
                }
            }
            treeView1.ExpandAll();
        }
        private void AddTreeNode(ref TreeNode parent, DataSet menus)
        {
            foreach (DataRow row in menus.Tables[0].Rows)
            {
                if (Convert.ToInt32(row["PARENT_ID"]) == Convert.ToInt32(parent.Name))
                {
                    TreeNode child = new TreeNode();
                    child.Text = row["MENU_TEXT"].ToString();
                    child.Name = row["MENU_ID"].ToString();
                    if (row["FORM_NAME"] != DBNull.Value)
                    {
                        child.Tag = new string[2] { row["FORM_NAME"].ToString(), row["PARAMETER"].ToString() };
                        child.StateImageIndex = 1;
                    }
                    else
                    {
                        child.StateImageIndex = 0;
                    }

                    parent.Nodes.Add(child);
                    AddTreeNode(ref child, menus);
                }
            }
        }

        private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node.Nodes.Count > 0)
                return;
            if (e.Node.Tag != null)
            {
                string[] node_tag = (string[])e.Node.Tag;

                foreach (DockContent childForm in this.DockPanel.Contents)
                {
                    if (childForm.Name == "ReportScreen")
                    {
                        childForm.Text = e.Node.Text;
                        childForm.Tag = node_tag[1];
                        ((ReportScreen)childForm).ReportName = node_tag[0];
                        ((ReportScreen)childForm).ResetConfig();
                        childForm.Activate();
                        return;
                    }
                }

                ReportScreen obj = new ReportScreen();
                //DockContent obj = (DockContent)Activator.CreateInstance(
                //    Type.GetType(node_tag[0], true, true));
                obj.Text = e.Node.Text;
                obj.Tag = node_tag[1];
                obj.ReportName = node_tag[0];
                obj.Show(this.DockPanel, DockState.DockBottom);
            }
        }

    }
}